//
//  CZMainTabBar.swift
//  CZWeibo
//
//  Created by 周振兰 on 16/5/10.
//  Copyright © 2016年 周振兰. All rights reserved.
//

import UIKit

class CZMainTabBar: UITabBar {
    //MARK: - 属性
    /// 2.加号按钮调用闭包
    var composeCallback:(()->())?
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //计算frame的宽度
        let width = self.frame.width/5
        //记录按钮的位置
        var index = 0
        for view in self.subviews
        {
            if view.isKindOfClass(NSClassFromString("UITabBarButton")!)
            {
                // 能到这里面来说明view 是 UITabBarButton类型
                // 重新设置frame
                view.frame = CGRect(x:CGFloat(index)*width, y:0,  width:width, height:self.frame.height)
                index++
                if index == 2
                {
                    index++
                }
                //设置添加好的按钮
                composeButton.frame = CGRectMake(2*width, 0, width, self.frame.height)
            }
        }
        
        
    }
    func composeButtonClick(button:UIButton)
    {
        //3.调用闭包
        composeCallback?()
    }
    //MARK : - 懒加载
    lazy var composeButton: UIButton =
    {
        let button = UIButton(type: UIButtonType.Custom)
        //设置按钮
       
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        
         //设置背景图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        self.addSubview(button)
        //Selector
        button.addTarget(self, action: "composeButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
       return button
    }()
}
